package com.yc.web.servlets.admin;

import com.yc.model.JsonModel;
import com.yc.web.servlets.BaseServlet;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.*;

/**
 * @author zwei
 * @create 2024-07-09 18:35
 */
@WebServlet("/admin/reviews.action")
public class ReviewsServlet extends BaseServlet {

    public void getreview(HttpServletRequest req,HttpServletResponse resp) throws IOException {
        int id= Integer.parseInt(req.getParameter("id"));
        String sql="select re.*,user.nickname,food.food_name from tb_food_reviews re\n" +
                "                 join tb_user user on re.userid=user.id\n" +
                "                  join tb_food food on re.foodid=food.id where re.id=?";
        List<Map<String,Object>>list=dbHelper.select(sql,id);
        JsonModel jm=new JsonModel();
        jm.setCode(1);
        jm.setObj(list.get(0));
        super.writeJson(jm,resp);
    }
    public void getAllReviews(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String sql1="select re.*,user.nickname,food.food_name from tb_food_reviews re \n" +
                "  join tb_user user on re.userid=user.id \n" +
                "  join tb_food food on re.foodid=food.id where 1=1";
        String sql2="select count(*) as total from (select re.*,user.nickname,food.food_name from tb_food_reviews re \n" +
                "  join tb_user user on re.userid=user.id \n" +
                "  join tb_food food on re.foodid=food.id where 1=1";
        int page=1;
        int limit = 10;
        List<Object> params = new ArrayList<>();
        if (req.getParameter("page")!=null){
            page= Integer.parseInt(req.getParameter("page"));
        }
        if (req.getParameter("limit")!=null){
            limit= Integer.parseInt(req.getParameter("limit"));
        }
        int skip=(page-1)*limit;
        if (req.getParameter("nickname")!=null&&!"".equals(req.getParameter("nickname"))){
            sql1+=" and nickname=? ";
            sql2+=" and nickname=? ";
            params.add(req.getParameter("nickname"));
        }
        if (req.getParameter("food_name")!=null&&!"".equals(req.getParameter("food_name"))){
            sql1+=" and food_name=? ";
            sql2+=" and food_name=? ";
            params.add(req.getParameter("food_name"));
        }
        System.out.println(req.getParameter("nickname"));
        System.out.println(req.getParameter("food_name"));
        sql2=sql2+") as result ";
        List<Map<String,Object>>list=dbHelper.select(sql1+" order by re.createdate DESC  limit "+limit+" offset "+skip,params.toArray());
        System.out.println(sql2);
        List<Map<String,Object>>result=dbHelper.select(sql2,params.toArray());
        System.out.println(result);
        int total= Integer.parseInt(result.get(0).get("total").toString());
        Map<String,Object>map=new HashMap<>();
        map.put("code",0);
        map.put("msg","查询成功");
        map.put("data",list);
        map.put("count",total);
        super.writeObjectToJson(map,resp);
    }
    public void reviewReturn(HttpServletRequest req,HttpServletResponse resp) throws IOException {
        JsonModel jm=new JsonModel();
        int review_id= Integer.parseInt(req.getParameter("id"));
        String reply_text=req.getParameter("reply_text");
        String sql="insert into tb_review_replies(review_id, user_id, reply_text, createdate, disabled) VALUES (?,?,?,?,?)";
        int i=dbHelper.doUpdate(sql,review_id,27,reply_text, LocalDateTime.now(),0);
        if (i!=0){
            jm.setCode(1);
        }else {
            jm.setCode(0);
            jm.setError("回复失败");
        }
        super.writeJson(jm,resp);
    }
    public void del(HttpServletRequest req,HttpServletResponse resp) throws IOException {
        String id = req.getParameter("idsStr");
        String[] parts = id.split(",");
        String sql="delete from tb_food_reviews where id=?";
        for (String str : parts) {
            dbHelper.doUpdate(sql,str);
        }
        Map<String,Object>map=new HashMap<>();
        map.put("code",0);
        map.put("msg","删除成功");
        super.writeObjectToJson(map,resp);
    }
    public void reviews(HttpServletRequest req,HttpServletResponse resp) throws IOException {
        String id = req.getParameter("idsStr");
        String[] parts = id.split(",");
        for (String str:parts){
            reviewtol(str);
        }
        Map<String,Object>map=new HashMap<>();
        map.put("code",0);
        map.put("msg","一键回复成功");
        super.writeObjectToJson(map,resp);
    }
    public void reviewtol(String id){
        String sql1="insert into tb_review_replies(review_id, user_id, reply_text, createdate, disabled) VALUES (?,?,?,?,?)";
        dbHelper.doUpdate(sql1,id,27,getRandomResponse(),LocalDateTime.now(),0);
    }
    public static String getRandomResponse() {
        String[] responses = {
                "谢谢亲！",
                "感谢你的评价！",
                "我们已经收到你的评价,后续会更加努力做好的！",
                "非常赞！",
                "感谢感谢感谢！",
                "你的评价真的太好了！",
                "66666666666！",
                "无敌的，下次来给你打折！",
        };
        Random random = new Random();
        int index = random.nextInt(responses.length);
        return responses[index];
    }

}

